﻿/* ---------------------------------------
# SK website Project
# common util
# 2010.8.4
----------------------------------------*/
var AspControlUtil = function(rIdPrefix){
    var __IDPrefix = rIdPrefix;
    this.IdPrefix = function(){
        return __IDPrefix;
    };
    this.setPrefix = function(v){
        __IDPrefix = v;
    };
    //ASP:GridView控件
    this.gv = new function(){
        //将xml格式的数据转换成字典
        var __ParseGridRow = function(data){
            var doc = null;
            var nl = null;
            var val = new Array();
            try{
                doc = jskitXml.loadXml(data);
                nl = doc.documentElement.childNodes;
                for(var i=0;i<nl.length;i++){
                    val[nl[i].nodeName] = nl[i].text;
                }
            }catch(e){
                val = null;
            }
            doc = nl = null;
            return val;
        };
        //将xml格式的数据转换成json
        var __ParseGridRow2Json = function(data){
            var doc = null;
            var nl = null;
            var val = new Array();
            try{
                doc = jskitXml.loadXml(data);
                val = jskitXml.doc2Json(doc);
            }catch(e){
                val = null;
            }
            doc = nl = null;
            return val;
        };
        //选中所有行
        this.selectAll = function(sender,e){
            var tab = sender;
            try{
                while(tab.tagName.toLowerCase()!="table"){
                    tab = tab.parentNode;
                }
                var flag = tab.getAttribute("id");
                var cbs = document.getElementsByTagName("input");
                if(cbs!=null && cbs.length>0){
                    for(var i=0;i<cbs.length;i++){
                        if(cbs[i].getAttribute("type").toLowerCase()=="checkbox" && cbs[i].getAttribute("id").indexOf(flag)!=-1){
                            cbs[i].checked = sender.checked;
                        }
                    }
                }
            }catch(e){};
        };
        //获取选中的行数
        this.getSelectedRowCount = function(tableId){
            var count = 0;
            try{
                var cbs = document.getElementsByTagName("input");
                if(cbs!=null && cbs.length>0){
                    for(var i=0;i<cbs.length;i++){
                        if(cbs[i].getAttribute("type").toLowerCase()=="checkbox" 
                        && cbs[i].getAttribute("id").indexOf(tableId)!=-1
                        && cbs[i].checked){
                            count++;
                        }
                    }
                }
            }catch(e){}
            return count;
        };
            
        //BEGIN action check
        //GridView控件中Row的点击事件
        var __GridRowCssClass = null;
        var __selectRow = null;
        this.onGridViewRowSelected = function(sender,e,targetId){
            if(__selectRow!=null){//first
                __selectRow.className = __GridRowCssClass;
            }
            __GridRowCssClass = sender.className;
            sender.className= "GridViewRowSelected";
            __selectRow = sender;
            var _url = sender.getAttribute("url");
            if(window.jagv==null){
                window.jagv = new JskitAjaxGridView("jagv");
            }
            //window.open(_url);
            jagv.load(_url,targetId);
        };
        this.selectGridView = function(rGridViewId,rIndex){
            var gv = $("#"+rGridViewId);
            var rows = gv.childNodes[0].childNodes;
            for(var i=1;i<rows.length;i++){
                if(i-1==rIndex){
                    rows[i].click();
                }
            }
        };
        this.checkModify = function(tabid){
            var count = this.getSelectedRowCount(tabid);
            if(count==1){
                return true;
            }else if(count>1){
                alert(unescape("%u9009%u5B9A%u7684%u8BB0%u5F55%u592A%u591A%uFF0C%u8BF7%u9009%u62E9%u4E00%u9879%u8981%u4FEE%u6539%u7684%u8BB0%u5F55%21"));
                return false;
            }else{
                alert(unescape("%u8BF7%u9009%u62E9%u4E00%u9879%u8981%u4FEE%u6539%u7684%u8BB0%u5F55"));
                return false;
            }
        };
        this.checkDelete = function(tabid){
            var count = this.getSelectedRowCount(tabid);
            if(count>0){
                return(confirm(unescape("%u786E%u5B9A%u8981%u5220%u9664%uFF1F"),unescape("%u5220%u9664")));
            }else{
                alert(unescape("%u8BF7%u9009%u62E9%u8981%u5220%u9664%u7684%u8BB0%u5F55"));
                return false;
            }
        };
        this.checkMultiSelect = function(tabid){
            var count = this.getSelectedRowCount(tabid);
            if(count>0){
                return true;
            }else{
                alert(unescape("%u8BF7%u9009%u62E9%u8981%u64CD%u4F5C%u7684%u8BB0%u5F55"));
                return false;
            }
        };
        this.checkSingleSelect = function(tabid){
            var count = this.getSelectedRowCount(tabid);
            if(count==1){
                return true;
            }else if(count>1){
                alert(unescape("%u9009%u5B9A%u7684%u8BB0%u5F55%u592A%u591A%uFF0C%u8BF7%u9009%u62E9%u4E00%u9879%u8981%u64CD%u4F5C%u7684%u8BB0%u5F55"));
                return false;
            }else{
                alert(unescape("%u8BF7%u9009%u62E9%u4E00%u9879%u8981%u64CD%u4F5C%u7684%u8BB0%u5F55"));
                return false;
            }
        };
        this.checkCbxDelete = function(rCbxName){
            var cl = $("@"+rCbxName);
            var checkCount = 0;
            if(cl!=null && cl.length>0){
                for(var i=0;i<cl.length;i++){
                    if(cl[i].checked==true){
                        checkCount++;
                    }
                }
            }
            if(checkCount>0){
                return(confirm(unescape("%u786E%u5B9A%u8981%u5220%u9664%uFF1F"),unescape("%u5220%u9664")));
            }else{
                alert(unescape("%u8BF7%u9009%u62E9%u8981%u5220%u9664%u7684%u8BB0%u5F55"));
                return false;
            }
        };
        this.checkCbxModify = function(rCbxName){
            var cl = $("@"+rCbxName);
            var checkCount = 0;
            if(cl!=null && cl.length>0){
                for(var i=0;i<cl.length;i++){
                    if(cl[i].checked==true){
                        checkCount++;
                    }
                }
            }
            if(checkCount==1){
                return true;
            }else if(checkCount>1){
                alert(unescape("%u9009%u5B9A%u7684%u8BB0%u5F55%u592A%u591A%uFF0C%u8BF7%u9009%u62E9%u4E00%u9879%u8981%u4FEE%u6539%u7684%u8BB0%u5F55%21"));
                return false;
            }else{
                alert(unescape("%u8BF7%u9009%u62E9%u4E00%u9879%u8981%u4FEE%u6539%u7684%u8BB0%u5F55"));
                return false;
            }
        };
    };
    //将数据赋值给对象
    var __bindProperty = function(ObjId,val){
        var obj = $("#"+ObjId);
        if(obj!=null){
            if(val==null || val=="undefined"){
                val="";
            }
            obj.value = val;
        }
    };

    //BEGIN! open a modal dialog
    var __OpenDialog = function(url,width,height){
        var w = (typeof(width)=="number")?width:500;
        var h = 300;
        var parms = "dialogWidth="+w+"px; dialogHeight="+h+"px;scroll=auto;location=no;status=no";
        if(url.indexOf("?")!=-1){
            url = url + "&g="+jskitUtil.guid();
        }else{
            url = url + "?g="+jskitUtil.guid();
        }
        return showModalDialog(BASE_URL+url ,window,parms);
    };
    //END!
    this.tree = new function(){
        this.GetSelectedTreeNodeCount = function(treeId){
            var count = 0;
            try{
                var cbs = document.getElementsByTagName("input");
                if(cbs!=null && cbs.length>0){
                    for(var i=0;i<cbs.length;i++){
                        if(cbs[i].getAttribute("type").toLowerCase()=="checkbox" 
                        && cbs[i].getAttribute("id").indexOf(treeId)!=-1
                        && cbs[i].checked){
                            count++;
                        }
                    }
                }
            }catch(e){}
            return count;
        };
        
         this.checkTreeDelete = function(treeId){
            var count = this.GetSelectedTreeNodeCount(treeId);
            if(count>0){
                return(confirm(unescape("%u786E%u5B9A%u8981%u5220%u9664%uFF1F"),unescape("%u5220%u9664")));
            }else{
                alert(unescape("%u8BF7%u9009%u62E9%u8981%u5220%u9664%u7684%u8BB0%u5F55"));
                return false;
            }
        };
    };
   
    
    //END!
    var __FixID = function(id){
        if(id.indexOf(__IDPrefix)!=0){
            return __IDPrefix + id;
        }else{
            return id;
        }
    };
    
    this.setReadOnly = function(objId){
        objId = __FixID(objId);
        var obj = $("#"+objId);
        if(obj!=null){
            obj.setAttribute("readonly","readonly");
        }
    };
};      